package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {
    //注解配置Mybatis
    // 创建一个方法获取SqlSessionFactoryBean对象 并存入ioc容器中
    @Bean //使用这个注解将获取的对象存入ioc容器             //这里由于在DbConfig中只传入一个datasource所有自动隐藏了一个@Autowired
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
         //传入dataSource
        ssfb.setDataSource(dataSource);
        //包别名
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        return ssfb;
    }

    //创建一个获取Mapper代理对象的对象MapperScannerConfigurer
    @Bean//将方法获取的对象存入IOC容器
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer mc=new MapperScannerConfigurer();
        mc.setBasePackage("com.itheima.mapper");
        return mc;
    }
}
